# -*- coding: utf-8 -*-
import cv2

def resize_and_pad(image, target_size=(800, 800), pad_color=0):
    # 获取原图尺寸
    old_size = image.shape[:2]
    print(old_size)
    ratio = min(float(target_size[0])/old_size[0], float(target_size[1])/old_size[1])
    new_size = tuple([int(x*ratio) for x in old_size])

    # 缩放图像
    im_resized = cv2.resize(image, (new_size[1], new_size[0]))

    # 计算需要填充的空间
    delta_w = target_size[1] - new_size[1]
    delta_h = target_size[0] - new_size[0]
    top, bottom = delta_h//2, delta_h-(delta_h//2)
    left, right = delta_w//2, delta_w-(delta_w//2)

    # 应用填充
    color = [pad_color]*3
    im_padded = cv2.copyMakeBorder(im_resized, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)

    return im_padded

# 加载图像
target_image = cv2.imread(r'E:\qywx\WXWork\1688855114377274\Cache\Image\2025-04\20250415045642.jpg')
# 调整大小并填充
result_image = resize_and_pad(target_image)
cv2.imwrite("resized1.jpg", result_image)
